import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  formDataMap: [],
  supervisionMatter:{},
};

const getters = {
  formDataMap: state => state.formDataMap,
  supervisionMatter: state => state.supervisionMatter,
};

const actions = {
  // 更新流程表单数据
  updateFormDataMap({ commit }, param) {
    commit(types.CACHE_FORM_DATA, param);
  },
  // 删除流程表单数据
  delFormDataMap({ commit }, param) {
    commit(types.CACHE_FORM_DATA, param);
  },
  //查看督办详情
  getSupervisionMatter({ commit }, param) {
    return Api.getSupervisionMatterInfoById(param).then(ret => {
      commit(types.SUPERVISE_MATTER_INFO, ret.data);
      return ret;
    });
  },
};

const mutations = {
  [types.SUPERVISE_MATTER_INFO](state, payload) {
    state.supervisionMatter = payload;
  },
  [types.CACHE_FORM_DATA](state, payload) {
    if (state.formDataMap) {
      let hasExisted = false;
      state.formDataMap.map(item => {
        if (item.key === payload.key) {
          hasExisted = true;
          item.obj = payload.obj;
        }
      });
      if (!hasExisted) {
        state.formDataMap.push(payload);
      }
    }
  },
  [types.DEL_CACHE_FORM_DATA](state, payload) {
    if (state.formDataMap) {
      state.formDataMap.map((item, index) => {
        if (item.key === payload.key) {
          state.formDataMap.splice(index, 1);
        }
      });
    }
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
